﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;

using alib.Debugging;

namespace agree
{
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	///
	/// 
	///
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public sealed partial class EntryMgr : EntryMgrTdlLoader
	{
		public Instance FindExpanded(Tfs e)
		{
			return AllInstances.FirstOrDefault(_e => _e.IsExpanded && _e.Expanded == e);
		}

		public Instance FindDefinition(Tfs e)
		{
			return AllInstances.FirstOrDefault(_e => _e.Definition == e);
		}

		public String FindInfo(Tfs e)
		{
			foreach (Instance inst in AllInstances)
			{
				if (inst.Definition == e)
					return "definition: " + inst.Name;
				if (inst.IsExpanded && inst.Expanded == e)
					return "expanded: " + inst.Name;
			}
			return "unknown";
		}

		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public IEnumerable<Instance> AllInstances
		{
			get { return ((IEnumerable<Instance>)bcpo.type_arr).Concat(AllEntries); }
		}


		void __find_inaccessible_types()
		{
			var bv = new System.Collections.BitArray(td.Count);
			foreach (var ent in AllEntries)
			{
				mark_types(bv, ent.InstanceType);
			}

			foreach (var typ in td.Values)
			{
				if (!(typ.Expanded is EmptyFeatureStructureTfs) && typ.IsLeaf && !bv[typ._id])
				{
					Debug.Print(typ.Name);
				}
			}

			//foreach (var ent in AllEntries)
			//{
			//	var typ = ent.InstanceType;
			//	if (!bv[typ._id])
			//		Debug.Print(typ.Name);
			//}

			Nop.X();
		}

		static void mark_types(System.Collections.BitArray bv, Type typ)
		{
			if (bv[typ._id])
				return;
			bv[typ._id] = true;
			foreach (var p in typ.parents)
				mark_types(bv, p);
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>
		/// 
		/// </summary>
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		//void FeatureModuliReport()
		//{
		//    foreach (FeatureConfig fc in fcm)
		//    {
		//        if (fc.Count == 0)
		//            continue;
		//        Console.WriteLine("{0,4} {1,2} [{2,3} {3,3}] ({4,3}) mod: {5,3}   {6}",
		//            fc.ConfigIndex,
		//            fc.Count,
		//            fc.min,
		//            fc.max,
		//            fc.range,
		//            fc.modulus,
		//            fc.Select(fix => fix.ToString().PadLeft(3)).StringJoin(" "));
		//        //	Console.WriteLine("                                   {0}", fc.Select(fix => (fix - fc.min).ToString().PadLeft(3)).StringJoin(" "));
		//        Console.WriteLine("                                   {0}", fc.Select(fix => (fix % fc.modulus).ToString().PadLeft(3)).StringJoin(" "));
		//    }
		//    Console.WriteLine("feats: {0} fcs: {1}  max feat/type: {2} max range: {3}", feat_arr.Length, fcm.Count, fcm.MaxFeaturesPerType, fcm.MaxFeatureRange);
		//}
	};
}
